<template>
  

  <div class="app-container" style="width:100%;height:100%;">
    <div class="block" style="width:100%;height:100%;">
     是否要按照目录生成MD文件
    </div>
    <span slot="footer" class="dialog-footer">
      <div style="text-align:center;margin-top:20px;">
        <el-button type="primary" @click="onSave">确定</el-button>
        <el-button type="danger" class="quxiao_btn" @click="closePage()">取消</el-button>
      </div>
    </span>
  </div>
</template>

<script>

import { getTreeList, makeMD } from '@/api/configMenu'
import { Message } from 'element-ui'

export default {
  name: 'Zzjg',
  props: { // 第二种方式
    xiangmu: {
      type: String,
      required: true
    },
    classify: {
      type: String,
      required: true
    },
    level: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      data: [],
      data2: [],
      ids: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    getTreeList() { // 加载列表
      getTreeList({ xiangmuId: this.xiangmu }).then(response => {
        this.data = response.data

        this.data2 = this.buildTree2(this.data)
      }).catch(response => {
      })
    },
    buildTree2(data) {
      var rdata = []
      for (let i = 0; i < data.length; i++) {
        var e1 = data[i]
        if (e1.parentId === '-1') {
          rdata.push(e1)
        }
        for (let j = 0; j < data.length; j++) {
          var e2 = data[j]
          if (e1.parentId === e2.id) {
            if (!e2.children) {
              e2.children = []
            }
            e2.children.push(e1)
          }
        }
      }
      return rdata
    },
    onSave() {
      var ids = ''
      var flag = ''
      var level = ''

      ids = this.classify
    
      console.log(this.level,'level')
      
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      makeMD({ id: ids, flag: flag, level:this.level }).then(response => {

        var fileName = 'download.zip'
        // const contentDisposition = response.headers['content-disposition']
        //   if (contentDisposition) {
        //     fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1], 'UTF-8')
        //   }
          const blob = new Blob([response], {
            type: `application/zip` // word文档为msword,pdf文档为pdf
          })
          const objectUrl = URL.createObjectURL(blob)
          const link = document.createElement('a')
          link.href = objectUrl
          console.log(objectUrl,'objectUrl')
          link.setAttribute('download', fileName)
          document.body.appendChild(link)
          link.click()
          // 释放内存
          window.URL.revokeObjectURL(link.href)
          loading.close()
          this.closePage()
        if (response.code === 20000) {
          Message({
              message: '生成成功',
              type: 'success',
              duration: 5 * 1000
            })
            loading.close()
            this.closePage()
        }
        
      }).catch(response => {
        loading.close()
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
